package com.qingfeng.music.ui.billboard.presenter;

import android.text.TextUtils;

import com.qingfeng.music.Constant;
import com.qingfeng.music.bean.PlayBean;
import com.qingfeng.music.bean.SongRankBean;
import com.qingfeng.music.dao.Music;
import com.qingfeng.music.ui.billboard.contract.BillboardContract;
import com.shuji.android.library.rx.RxSubscriber;
import com.shuji.android.library.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * 排行榜
 * <p>
 * Created by Ganlin.Wu on 2016/10/14.
 */
public class BillboardPresenter extends BillboardContract.Presenter {

    /**
     * 加载排行榜歌曲（调用model取得歌曲列表，然后调用view显示歌曲和背景）
     *
     * @param format
     //* @param callback
     * @param from
     * @param method
     * @param type
     * @param size
     * @param offset
     */
    @Override
    public void loadBillboardMusics(final String format,   String from, String method, int type, int size, int offset) {
        getRxManage().add(getModel()
                .getBillBoardMusics(format,  from, method, type, size, offset)
                .map(new Func1<SongRankBean, List<Music>>() { //SongRankBean 转化为 List<Music>
                    @Override
                    public List<Music> call(final SongRankBean bean) {

                        //1、切换背景图片
                        Observable.create(new Observable.OnSubscribe<String>() {
                            @Override
                            public void call(Subscriber<? super String> subscriber) {
                                subscriber.onNext(bean.getBillboard().getPic_s444());
                            }
                        }).subscribe(new RxSubscriber<String>(getContext(), true) {
                            @Override
                            protected void _onNext(String url) {
                                getView().showBackdrop(url); //调用view显示背景图片
                            }

                            @Override
                            protected void _onError(String message) {
                            }
                        });


                        //2、转化为List<Music>
                        List<SongRankBean.SongListBean> songList = bean.getSong_list();
                        List<Music> musics = new ArrayList<>();
                        for (SongRankBean.SongListBean song : songList) {

                            final Music music = new Music();
                            music.setId(Long.parseLong(song.getSong_id()));
                            music.setTitle(song.getTitle());
                            music.setArtist(song.getArtist_name());
                            music.setAlbum(song.getAlbum_title());
                            music.setAlbumId(Long.parseLong(song.getAlbum_id()));
                            music.setImageUrl(song.getPic_small());

                            //设置文件url
                            getRxManage().add(getModel()
                                    .getPlay(Constant.API_FORMAT,  Constant.API_FROM, Constant.API_METHOD_PLAY, song.getSong_id())
                                    .subscribe(new RxSubscriber<PlayBean>(getContext(), false) {
                                        @Override
                                        protected void _onNext(PlayBean playBean) {
                                            String url = playBean.getBitrate().getShow_link();
                                            if (TextUtils.isEmpty(url)) {
                                                url = playBean.getBitrate().getFile_link();
                                            }
                                            music.setPath(url); //设置url
                                        }

                                        @Override
                                        protected void _onError(String message) {
                                            LogUtils.loge(message);
                                        }
                                    }));

                            music.setLrcUrl(song.getLrclink());
                            musics.add(music);
                        }
                        return musics;
                    }
                }).subscribe(new RxSubscriber<List<Music>>(getContext(), false) {//显示视图
                    @Override
                    protected void _onNext(List<Music> musics) {
                        getView().showBillBoardMusics(musics); //调用view显示歌曲列表
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                })
        );
    }
}
